package com.tony.juc;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created with IntelliJ IDEA.
 *
 * @Title: ConcurrentHashMapDemo001
 * @Auther: 皮蛋布丁
 * @Date: 2022/04/16/13:33
 * @Description:
 */
public class ConcurrentHashMapDemo001 {

    public static void main(String[] args) {

        /*//出现并发修改异常ConcurrentModificationException
        Map<String,String> map = new HashMap<>(); //创建HashMap
        for (int num = 0; num < 10; num++) {
            new Thread(()->{
                for (int i = 0; i < 10; i++) {
                    map.put("【" + Thread.currentThread().getName() + "】i = " + i,"皮蛋布丁");
                    System.out.println(map); //输出全部的数据内容
                }
            },"集合操作线程_" + num).start();
        }*/

        Map<String,String> map = new ConcurrentHashMap<>(); //创建线程集合
        for (int num = 0; num < 10; num++) {
            new Thread(()->{
                for (int i = 0; i < 10; i++) {
                    map.put("【" + Thread.currentThread().getName() + "】i = " + i,"皮蛋布丁");
                    System.out.println(map);
                }
            },"集合操作线程_" + num).start();
        }

    }
}
